package com.cxria.script

import groovy.transform.ToString

class CmdUtils{

    Result execute(String cmd){
        def process = Runtime.getRuntime().exec(["sh", "-c", cmd] as String[])
        def successOut
        def errorOut
        Thread.start {
            successOut = process.inputStream.text
        }
        Thread.start {
            errorOut = process.errorStream.text
        }
        def exitCode = process.waitFor()
        def success
        if(exitCode == 0){
            success = true
        } else {
            success = false
        }
        def isPrint = false
        def result = new Result(success: success, successOut: successOut, errorOut: errorOut)
        if(isPrint){
            println cmd
            println result
        }
        result
    }

    @ToString(includePackage = false)
    class Result{
        boolean success
        String successOut
        String errorOut
    }

}
